// pages/website/website.js   
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbarIndex: 3,
    tabbar: [
      {
        img: 'https://crm.bxwhl.com/static/tabbar/index.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/index-select.png',
        text: '名片',
        url: '/pages/index/index',
      },
      {
        img: 'https://crm.bxwhl.com/static/tabbar/goods.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/goods-select.png',
        text: '产品',
        url: '/pages/product/product',
      },
      {
        img: 'https://crm.bxwhl.com/static/tabbar/dynamic.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/dynamic-select.png',
        text: '动态',
        url: '/pages/dynamic/dynamic',
      },
      {
        img: 'https://crm.bxwhl.com/static/tabbar/website.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/website-select.png',
        text: '官网',
        url: '/pages/website/website',
      },
       {
        img: 'https://crm.bxwhl.com/static/tabbar/mine.png',
        imgSelect: 'https://crm.bxwhl.com/static/tabbar/mine-select.png',
        text: '我',
        url: '/pages/mine/mine',
      },
    ],
    // 消息条数
    noteNum: 0,
    // 阅读百分比
    readPercent: 0,
  },
  todynamicLink:function(e){
    var article_id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/dynamicLink/dynamicLink?article_id='+article_id,
    })
  },
  toNews:function(){
    wx.navigateTo({
      url: '/pages/news/news',
    })
  },
  joinUs: function (e) {
    wx.navigateTo({
      url: '/pages/joinUs/joinUs?source=website&source_txt=来自官网联系我们',
    })
  },
  // 滚动计算阅读
  scroll: function (e) {
    var that = this;
    var scrollHeight = e.detail.scrollHeight;
    var scrollTop = e.detail.scrollTop;
    var windowHeight = that.data.windowHeight;
    var height = scrollHeight - windowHeight - 20;
    var scrollPercent = (scrollTop / height) * 100;
    scrollPercent = Math.ceil(scrollPercent);
    var readPercent = that.data.readPercent;
    if (scrollPercent > 100) {
      scrollPercent = 100;
      readPercent = 100; 
    } else {
      scrollPercent = scrollPercent;
      if (scrollPercent > readPercent) {
        readPercent = scrollPercent;
      } else {
        readPercent = readPercent;
      }
    }
    that.setData({
      readPercent: readPercent
    })
  },
  // tabbar跳转
  tabbarNav: function (e) {
    var url = e.currentTarget.dataset.url;
    wx.redirectTo({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  
  onLoad: function (options) {
    var that = this;
    app.httpRequest('user/website', {}, 'get', false, function(res){
      var article = res.data.enterprise.introduce
      WxParse.wxParse('article', 'html', article, that, 5);
      res.data.enterprise.introduce = article;
      that.setData({
        pageShow: true,
        website: res.data.enterprise,
        articleList: res.data.articleList.data,
      })
    })
  },

  getLocation: function (){
    var website = this.data.website;
    app.userBehavior(103,'查看了公司地址')
    wx.openLocation({
      latitude: parseFloat(website.latitude),
      longitude: parseFloat(website.longitude),
      name:website.name,
      address:website.detail,
      scale: 28
    })
  },
  makePhoneCall: function (e) {
    var phone = e.currentTarget.dataset.text;
    wx.makePhoneCall({
      phoneNumber: phone,
    })
  },
    

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var startTime = new Date().getTime();
    that.setData({
      startTime: startTime,
      windowHeight: wx.getStorageSync('systemInfo').windowHeight
    })
  },
  // 获得停留时间，毫秒级
  getStopTime: function () {
    var that = this;
    var endTime = new Date().getTime();
    var startTime = that.data.startTime;
    var stopTime = endTime - startTime;
    that.setData({
      stopTime: stopTime
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(125,'浏览了官网',stoptime,percent)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    that.getStopTime();
    var stoptime =  that.data.stopTime;
    var percent = that.data.readPercent;
    app.userBehavior(125,'浏览了官网',stoptime,percent)
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.userBehavior(126,'分享了官网')
    return {
      path: "/pages/website/website?share_card_id=" + wx.getStorageSync('share_card_id') + "&share_user_id=" + wx.getStorageSync('user_id'),
      success: function (a) {
      },
      fail: function (a) { }
    };
  }
})